<?php
require_once 'common/conn.php';
require_once 'common/common.php';
include_once "class.phpmailer.php";    //包函邮件发送类

$result_array = array();
$result_array['errorNo'] = "1";

if (isset($_GET['email'])) {
    $email = str_replace("__", "@", trim($_GET['email']));
    $db = new DBC();
    $sql = "SELECT devicecode AS usercode FROM t_device WHERE email = '" . $email . "'";
    
    $data = $db->fetchData($sql);
    if (checkArrayNotEmpty($data)) {
        $usercode = $data[0]['usercode'];
        $sql = "UPDATE t_device SET passwordTime = now(),passwordFlag = 0 WHERE email = '" . $email . "'";
        if ($db->execute($sql)) {
            $subject = "《嗨！宝贝》帐号--邮箱找回密码";
            $content = "我们收到您的《嗨！宝贝》账号密码重设要求，请点击下面的链接重设您的密码：<br>"
                    . "http://hibaby.sharemysound.com/server/password-reset.php?usercode=" . $usercode . "<br>"
                    . "(如果您无法点击此链接，请将它复制到浏览器地址栏后访问)<br>"
                    . "为了保证您帐号的安全，该链接有效期为24小时，并且点击一次后失效！<br>"
                    . "如您不需要做密码重设，或者您没有提出此要求，请忽视或删除此邮件。<br>"
                    . "如果您对您的《嗨！宝贝》账号有任何问题，可通过官方QQ群71123337咨询，客服电话：+86 28 85148237。<br>"
                    . "<br>"
                    . "《嗨！宝贝》制作团队 敬启<br>"
                    . "感谢您的支持！<br>"
                    . date("Y") . "年" . date("n") . "月" . date("j") . "日<br>"
                    . "------------------------------------<br>"
                    . "乐知官网：http://www.lz06.com<br>"
                    . "乐知幼教 互动更有效<br>";


            if (send_mail("hibaby@lz06.com", $email, $subject, $content)) {
                $result_array['errorNo'] = "0";
            } else {
                $result_array['errorMsg'] = "发送邮件失败";
            }
        } else {
            $result_array['errorMsg'] = "重置密码错误";
        }
    }else{
        $result_array['errorMsg'] = "邮箱 ".  $email." 不存在";
    }
} else {
    $result_array['errorMsg'] = "未输入邮箱地址";
}
echo json_encode($result_array);

function send_mail($frommail, $tomail, $subject, $body) {
    $mail = new PHPMailer();
    $mail->IsSMTP();                                // 经smtp发送
    $mail->Host = "smtp.exmail.qq.com";             // SMTP 服务器
    $mail->SMTPAuth = true;                         // 打开SMTP 认证
    $mail->Username = "hibaby@lz06.com";         // 用户名
    $mail->Password = "123qwe";                   // 密码
    $mail->From = $frommail;                        // 发信人
    $mail->FromName = "=?UTF-8?B?".base64_encode("嗨宝贝")."?=";                  // 发信人别名
    $mail->AddAddress($tomail);                     // 收信人
    $mail->Port = 25;
    $mail->WordWrap = 50;
    $mail->IsHTML(true);                            // 以html方式发送
    $mail->Subject = "=?UTF-8?B?".base64_encode($subject)."?=";                      // 邮件标题
    $mail->Body = $body;                            // 邮件内空
    $mail->AltBody = "请使用HTML方式查看邮件。";
    $mail->CharSet = "utf-8";
    return $mail->Send();
}
?>
